package mainPackage;

import java.awt.Dimension;
import java.util.ArrayList;

import objects.Ball;
import objects.Square;

public class CollisionMap {

	Dimension size;
	ArrayList<Square> collisionBoxes;

	public CollisionMap(Dimension size) {
		collisionBoxes = new ArrayList<Square>();
		this.size = size;
	}

	public void addCollisionBox(Square square) {
		collisionBoxes.add(square);
	}

	public boolean checkCollision(Ball ball) {
		int x = (int) (ball.getxPos() - ball.getRadius());
		int y = (int) (ball.getyPos() - ball.getRadius());
		int width = (int) (2 * ball.getRadius());
		int height = (int) (2 * ball.getRadius());

		for (int i = 0; i < collisionBoxes.size(); i++) {
			if (x + width > collisionBoxes.get(i).getxPos()
					&& x < collisionBoxes.get(i).getxPos()+collisionBoxes.get(i).getWidth()
					&& y + height > collisionBoxes.get(i).getyPos()
					&& y < collisionBoxes.get(i).getyPos()+collisionBoxes.get(i).getHeight()) {
				int distLeft = x + width - collisionBoxes.get(i).getxPos();
				int distRight = collisionBoxes.get(i).getxPos()+collisionBoxes.get(i).getWidth() - x;
				int distTop = y + height - collisionBoxes.get(i).getyPos();
				int distDown = collisionBoxes.get(i).getyPos()+collisionBoxes.get(i).getHeight() - y;
				boolean leftNear = ball.getxSpeed() > 0;
				boolean topNear = ball.getySpeed() > 0;
				if(leftNear){
					if(topNear){
						if(distTop < distLeft){
							//TOP
							y = collisionBoxes.get(i).getyPos()-height;
							ball.setyPos(y + ball.getRadius());
							ball.setySpeed(ball.getySpeed() * -1);
						}else{
							//LEFT
							x = collisionBoxes.get(i).getxPos()-width;
							ball.setxPos(x+ball.getRadius());
							ball.setxSpeed(ball.getxSpeed()*-1); 
						}
					} else {
						if(distDown < distLeft){
							//DOWN
							y = collisionBoxes.get(i).getyPos()+collisionBoxes.get(i).getHeight();
							ball.setyPos(y + ball.getRadius());
							ball.setySpeed(ball.getySpeed() * -1);
						}else{
							//LEFT
							x = collisionBoxes.get(i).getxPos()-width;
							ball.setxPos(x+ball.getRadius());
							ball.setxSpeed(ball.getxSpeed()*-1); 
						}
					}
				}else{
					if(topNear){
						if(distTop < distRight){
							//TOP
							y = collisionBoxes.get(i).getyPos()-height;
							ball.setyPos(y + ball.getRadius());
							ball.setySpeed(ball.getySpeed() * -1);
						}else{
							//RIGHT
							x = collisionBoxes.get(i).getxPos()+collisionBoxes.get(i).getWidth();
							ball.setxPos(x+ball.getRadius()); 
							ball.setxSpeed(ball.getxSpeed()*-1);
						}
					} else {
						if(distDown < distRight){
							//DOWN
							y = collisionBoxes.get(i).getyPos()+collisionBoxes.get(i).getHeight();
							ball.setyPos(y + ball.getRadius());
							ball.setySpeed(ball.getySpeed() * -1);
						}else{
							//RIGHT
							 x = collisionBoxes.get(i).getxPos()+collisionBoxes.get(i).getWidth();
							 ball.setxPos(x+ball.getRadius()); 
							 ball.setxSpeed(ball.getxSpeed()*-1);  
						}
					}
				}
				
				return true;
			}
		}

		if (x <= 0) {
			ball.setxPos(ball.getRadius());
			if (ball.getxSpeed() < 0) {
				ball.setxSpeed(ball.getxSpeed() * -1);
			}
		}
		if (y <= 0) {
			ball.setyPos(ball.getRadius());
			if (ball.getySpeed() < 0) {
				ball.setySpeed(ball.getySpeed() * -1);
			}
		}
		if (x >= size.width - 2*ball.getRadius()) {
			ball.setxPos(size.width - ball.getRadius());
			if (ball.getxSpeed() > 0) {
				ball.setxSpeed(ball.getxSpeed() * -1);
			}
		}
		if (y >= size.height - 2*ball.getRadius()) {
			ball.setyPos(size.height - ball.getRadius());
			if (ball.getySpeed() > 0) {
				ball.setySpeed(ball.getySpeed() * -1);
			}
		}

		return true;
	}
}
